package com.bawei.shoppingcar.listviewfood_demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.shoppingcar.R;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 1.自定义类继承BaseAdapter并重写4个方法
 * 2.定义2个成员变量
 */
public class FoodAdapter extends BaseAdapter {
    private List<FoodEntity.DataBean> list;//数据源
    private Context context;//上下文
    public FoodAdapter(List<FoodEntity.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }
    //返回个数
    @Override
    public int getCount() {
        return list.size();
    }
    //返回对应的数据
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    //返回下标
    @Override
    public long getItemId(int position) {
        return position;
    }
    //返回对应的itemView
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_food, null);
            viewHolder.iv = convertView.findViewById(R.id.iv);
            viewHolder.tv = convertView.findViewById(R.id.tv);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //文字
        viewHolder.tv.setText(list.get(position).getTitle()+"");//注意：必须是String类型
        //图片：load(网址).into(imageView);
        Glide.with(context).load(list.get(position).getPic()).into(viewHolder.iv);
        return convertView;
    }
    class ViewHolder{
        ImageView iv;
        TextView tv;
    }
}










